package day002;

import java.util.Stack;


public class Code03_IsPalindromeList {

    public static class Node{
        public int value;
        public Node next;

        public Node(int data){
            this.value = data;
        }
    }

    // 压栈，需要额外空间 N
    public static boolean isPalindrome1(Node head){
        Stack<Node> stack = new Stack<>();
        Node  cur = head;
        while(cur != null){  //全部压栈
            stack.push(cur);
            cur = cur.next;
        }

        while(head != null){
            if(head.value != stack.pop().value){
                return false;
            }
            head = head.next;
        }

        return true;
    }

    // 后一半压栈，额外空间 N/2
    public static boolean isPalindrome2(Node head){
        if(head == null || head.next == null){
            return true;
        }
        //快慢指针
        Node right = head.next;
        Node cur = head;
        while(cur.next !=null && cur.next.next != null){
            right = right.next;
            cur = cur.next.next;
        }
        Stack<Node> stack = new Stack<>();
        while(right != null){
            stack.push(right);
            right = right.next;
        }

        while(!stack.isEmpty()){
            if(head.value != stack.pop().value){
                return false;
            }
            head = head.next;
        }
        return true;
    }

    // 空间复杂度 O(1)
    public static boolean isPalindrome3(Node head){

        if(head == null || head.next == null){
            return true;
        }
        //快慢指针遍历
        Node n1 = head;
        Node n2 = head;
        while(n2.next != null && n2.next.next != null){
            n1 = n1.next;      //n1 -> mid
            n2 = n2.next.next; //n2 -> end
        }
        // 实现后半部分指针逆转
        n2 = n1.next; //此时n2是反转后的尾部
        n1.next = null;
        Node n3 = null; //nextTmp
        while(n2 != null){
            n3 = n2.next;
            n2.next = n1;
            n1 = n2;
            n2 = n3;
        }
        n3 = n1; // 此时n3是新的头部
        n2 = head; // 此时n2是原来的头部
        boolean res = true;
        while(n1 != null && n2 != null){
            if(n1.value != n2.value){
                res = false;
                break;
            }
            n1 = n1.next;
            n2 = n2.next;
        }

        // 返回前恢复逆序
        n1 = n3.next;
        n3.next = null;
        while(n1 != null){
            n2 = n1.next;
            n1.next = n3;
            n3 = n1;
            n1 = n2;
        }

        return res;
    }
    public static void printList(Node head){
        while(head != null){
            System.out.print(head.value+(head.next!=null ? "->" : "\n"));
            head = head.next;
        }
    }


    public static void main(String[] args) {
        Node n1 = new Node(1);
        Node n2 = new Node(2);
        Node n3 = new Node(3);
        Node n4 = new Node(2);
        Node n5 = new Node(1);
        //Node n6 = new Node(1);
        n1.next = n2;
        n2.next = n3;
        n3.next = n4;
        n4.next = n5;
        //n5.next = n6;
        printList(n1);
        boolean ok = isPalindrome3(n1);
        System.out.println(ok);
        printList(n1);
    }
}
